package com.huibo.aigou.zuul.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class BaseConfig {

	@Bean
	public Docket getDocket() {
		return new Docket(DocumentationType.SWAGGER_2)
				    .apiInfo(getApi());
	}
	
	private ApiInfo getApi() {
		return new ApiInfoBuilder()
				   .title("分布式购物系统")
				   .description("购物系统接口文档说明")
				   .termsOfServiceUrl("http://localhost:9001")
				   .contact(new Contact("liwei", "", "liwei@163.com"))
				   .version("1.0")
				   .build();
	}
	
	
	@Bean
	public  CorsFilter getCorsFilter() {
		CorsConfiguration ccf = new CorsConfiguration();
		
		ccf.addAllowedOrigin("http://127.0.0.1:8080");
		ccf.addAllowedOrigin("http://localhost:8080");
		
		ccf.addAllowedMethod("OPTIONS");
		ccf.addAllowedMethod("HEAD");
		ccf.addAllowedMethod("GET");
		ccf.addAllowedMethod("PUT");
		ccf.addAllowedMethod("POST");
		ccf.addAllowedMethod("DELETE");
		ccf.addAllowedMethod("PATCH");
		
		ccf.addAllowedHeader("*");
		
		UrlBasedCorsConfigurationSource ucs = new UrlBasedCorsConfigurationSource();
		ucs.registerCorsConfiguration("/**", ccf);
		
		return new CorsFilter(ucs);
	}

}
